package club.xshiwu.springcloud.service.impl;

import club.xshiwu.springcloud.mapper.OrderMapper;
import club.xshiwu.springcloud.pojo.Order;
import club.xshiwu.springcloud.service.AccountService;
import club.xshiwu.springcloud.service.OrderService;
import club.xshiwu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Intellij IDEA.
 * Date:  2020/6/12
 * @author xsw
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @Override
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("开始创建订单---->");
        orderMapper.create(order);
        log.info("订单微服务开启调用库存，开始扣减---->");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("库存扣减完毕，正在准备下一步操作");

        log.info("订单微服务开启调用账户微服务，开始金额扣减");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("账户金额扣减完毕,正在更新订单状态中");
        orderMapper.update(order.getUserId(), 0);

    }
}
